package com.cgbl.service.ui

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.service.BR
import com.cgbl.service.R
import com.cgbl.service.adapter.AddTeamMemberAdapter
import com.cgbl.service.adapter.AddTeamTagAdapter
import com.cgbl.service.databinding.AppActivityTeamManagerBinding
import com.cgbl.service.vm.TeamManagerViewModel
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.MemberAdapterBean
import com.robot.base.util.LiveDataEventManager
import com.robot.base.util.arouter.ARouterUtil
import me.goldze.mvvmhabit.binding.command.BindingCommand

@Route(path = AppConstants.Router.Project.A_MEMBER_MANAGER)
class MemberListActivity : GLBaseActivity<AppActivityTeamManagerBinding, TeamManagerViewModel>() {

    lateinit var addTeamTagAdapter: AddTeamMemberAdapter

    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_team_manager

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initTiterBar()
        addTeamTagAdapter = AddTeamMemberAdapter(false)
        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = addTeamTagAdapter
        }
        addTeamTagAdapter.setOnItemClickListener { _, _, position ->
            val id = addTeamTagAdapter.data[position].accountId
            ARouterUtil.goToActivityWithBundle(AppConstants.Router.Project.A_PERSONAL_PROFILE,
                Bundle().apply {
                    putString(AppConstants.BundleKey.ID, id)
                })
        }
        viewModel.fetchHomeMember()
        binding.ivAdd.setOnClickListener {
            ARouterUtil.goToActivity(AppConstants.Router.Project.A_ADD_MEMBER)
        }
    }

    private fun initTiterBar() {
        val titleView = findViewById<TextView>(R.id.tv_title)
        val backView = findViewById<ImageView>(R.id.iv_back)
        val rightView = findViewById<TextView>(R.id.tv_right_text)
        rightView?.text = "标签管理"
        rightView?.visibility = View.VISIBLE
        titleView?.text = "人员管理"
        backView?.setOnClickListener { finish() }
        rightView?.setOnClickListener {
            ARouterUtil.goToActivity(AppConstants.Router.Project.A_TAG_MANAGER)
        }
    }

    override fun initViewObservable() {
        LiveDataEventManager.observerRefreshMemberEvent(this) {
            viewModel.fetchHomeMember()
        }
        viewModel.uc.teamMemberDataEvent.observe(this) {
            addTeamTagAdapter.setNewInstance(it as MutableList<MemberAdapterBean>?)
        }
    }
}